{{define "admin/managerexams.html"}}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>TM - Class</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!-- Bootstrap 3.3.7 -->
    <link rel="stylesheet" href="/static/libs/bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="/static/libs/font-awesome/css/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="/static/libs/Ionicons/css/ionicons.min.css">
    <!-- DataTables -->
    <link rel="stylesheet" href="/static/libs/datatables.net-bs/css/dataTables.bootstrap.min.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="/static/libs/AdminLTE/css/AdminLTE.min.css">
    <!-- AdminLTE Skins. Choose a skin from the css/skins
         folder instead of downloading all of them to reduce the load. -->
    <link rel="stylesheet" href="/static/libs/AdminLTE/css/skins/_all-skins.min.css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <link rel="stylesheet" href="/static/libs/sweetalert/sweetalert.css" type="text/css"/>
    <link rel="stylesheet" href="/static/libs/jbox/jBox.css">
    <link rel="stylesheet" href="/static/libs/iCheck/flat/_all.css" type="text/css"/>
    <link rel="stylesheet" href="/static/libs/daterangepicker/daterangepicker-bs3.css" type="text/css"/>


    <![endif]-->
    <link rel="stylesheet" href="/static/libs/daterangepicker/daterangepicker-bs3.css" type="text/css"/>
    <link rel="stylesheet" href="/static/libs/select2/css/select2.min.css" type="text/css"/>
    <link rel="stylesheet" href="/static/libs/iCheck/flat/_all.css" type="text/css"/>
    <link rel="stylesheet" href="/static/libs/sweetalert/sweetalert.css" type="text/css"/>
    <!-- Google Font -->
    <link rel="stylesheet" href="/static/font/Souce+Sans.css" type="text/css"/>

    <link rel="stylesheet" href="/static/libs/bootstrap-notify-master/css/bootstrap-notify.css">
    <link rel="stylesheet" href="/static/libs/bootstrap-notify-master/css/styles/alert-blackgloss.css">


    <script src="/static/libs/moment/moment.min.js"></script>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

{{template "admin/navbar.html" .}}
    <aside class="main-sidebar">
        <!-- sidebar: style can be found in sidebar.less -->
        <section class="sidebar">
            <!-- Sidebar user panel -->
            <div class="user-panel">
                <div class="pull-left image">
                {{if gt (len .user.AvatarUrl) 0}}
                    <img src="{{.user.AvatarUrl}}" class="img-circle" alt="User Image">
                {{else}}
                    <img src="/static/libs/AdminLTE/img/avastar.jpg" class="img-circle" alt="User Image">
                {{end}}
                </div>
                <div class="pull-left info">
                    <p>{{.user.Email}}</p>
                    <!--<a href="#"><i class="fa fa-circle text-success"></i> Online</a>-->
                </div>
            </div>
            <!-- sidebar menu: : style can be found in sidebar.less -->
            <ul class="sidebar-menu" data-widget="tree">
                <li class="header">MAIN NAVIGATION</li>
                <li class="active">
                    <a href="/admin/index">
                        <i class="fa fa-dashboard"></i> <span>总览</span>
                    </a>
                </li>


                <li class="treeview">
                    <a href="#">
                        <i class="fa fa-th"></i>
                        <span>班级</span>
                        <span class="pull-right-container">
              <i class="fa fa-angle-left pull-right"></i>
            </span>
                    </a>

                    <ul class="treeview-menu">
                        <li><a href="/admin/seeclass"><i class="fa fa-circle-o"></i> 查看班级</a></li>
                        <li><a href="/admin/managerclass"><i class="fa fa-circle-o"></i> 管理班级</a></li>
                    </ul>
                </li>


                <li class="treeview active">
                    <a href="#">
                        <i class="fa fa-laptop"></i>
                        <span>测试</span>
                        <span class="pull-right-container">
              <i class="fa fa-angle-left pull-right"></i>
            </span>
                    </a>

                    <ul class="treeview-menu">
                        <li><a href="/admin/getexams"><i class="fa fa-circle-o"></i> 查看小测</a></li>
                        <li class="active"><a href="/admin/managerexam"><i class="fa fa-circle-o"></i> 管理小测</a></li>
                    </ul>
                </li>

                <li class="treeview">
                    <a href="#">
                        <i class="fa fa-folder"></i>
                        <span>公告</span>
                        <span class="pull-right-container">
              <i class="fa fa-angle-left pull-right"></i>
            </span>
                    </a>

                    <ul class="treeview-menu">
                        <li><a href="/admin/seenotice"><i class="fa fa-circle-o"></i> 查看公告</a></li>
                        <li><a href="/admin/managernotice"><i class="fa fa-circle-o"></i> 管理公告</a></li>
                    </ul>
                </li>
            </ul>
        </section>
        <!-- /.sidebar -->
    </aside>
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <div class='notifications top-right'></div>
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                <small>测试管理</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="/admin/seeclass"><i class="fa fa-dashboard"></i> 测试</a></li>
                <li class="active"><a href="#">测试管理</a></li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="box">
                        <div class="box-header">
                            <h3 class="box-title">未发布测试</h3>
                        </div>
                        <div class="box-body">
                            <table id="example3" class="table table-bordered table-hover">
                                <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>测试名称</th>
                                    <th>测试描述</th>
                                    <th>难易系数</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                {{range .unpublishexams}}
                                <tr>
                                    <td name="{{.ExamID}}">{{.ExamID}}</td>
                                {{/*暂时不做详情页*/}}
                                {{/*<td><a href="/admin/class/{{.ID}}" target="_blank">{{.Title}}</a></td>*/}}
                                    <td>{{.Title}}</td>
                                    <td>
                                    {{.DesCrib}}
                                        {{/*<a href="javascript:void(0);" > </a>*/}}
                                    </td>
                                    <td>{{.DegreeName}}</td>
                                    <td>
                                        <a href="#" data-href="/admin/exam/update"
                                           class="btn btn-primary" data-id="{{.ExamID}}" data-name="{{.Title}}"
                                           data-desc="{{.DesCrib}}" data-flag="unpub" data-toggle="modal"
                                           data-target="#confirm-edit">修改</a>
                                    <a href="#" class="btn btn-danger" data-id="{{.ExamID}}"
                                    data-href="/admin/exam/publish"
                                    data-toggle="modal" data-target="">下载</a>
                                    </td>
                                </tr>
                                {{end}}
                            </table>
                        </div>
                        <!-- /.box-body -->
                    </div>
                    <!-- /.box -->
                    <div class="box">
                        <div class="box-header">
                            <h3 class="box-title">已发布测试</h3>
                        </div>
                        <div class="box-body">
                            <table id="example2" class="table table-bordered table-hover">
                                <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>测试名称</th>
                                    <th>测试描述</th>
                                    <th>难易系数</th>
                                    <th>开始时间</th>
                                    <th>结束时间</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                {{range .publishexams}}
                                <tr>
                                    <td name="{{.ExamID}}">{{.ExamID}}</td>
                                {{/*暂时不做详情页*/}}
                                {{/*<td><a href="/admin/class/{{.ID}}" target="_blank">{{.Title}}</a></td>*/}}
                                    <td>{{.Title}}</td>
                                    <td>
                                    {{.DesCrib}}
                                        {{/*<a href="javascript:void(0);" > </a>*/}}

                                    </td>
                                    <td>{{.DegreeName}}</td>
                                    <td>{{ .StartTime }}</td>
                                    <td>{{ .EndTime }}</td>
                                    <td>
                                        <a href="#" data-href="/admin/exam/update"
                                           class="btn btn-primary" data-id="{{.ExamID}}" data-name="{{.Title}}"
                                           data-desc="{{.DesCrib}}" data-flag="pub" data-st="{{.StartTime}}"
                                           data-et="{{.EndTime}}" data-toggle="modal"
                                           data-target="#confirm-edit">修改</a>
                                        <a href="#" class="btn btn-danger" data-id="{{.ExamID}}"
                                           data-href="/admin/exam/unpublish"
                                           data-toggle="modal" data-target="#confirm-pub">取消发布</a>
                                        <a href="#" class="btn btn-danger" data-id="{{.ExamID}}"
                                           data-href="/admin/exam/"
                                           data-toggle="modal" data-target="">下载</a>
                                    </td>
                                    </td>
                                </tr>
                                {{end}}
                            </table>
                        </div>
                        <!-- /.box-body -->
                    </div>
                </div>
                <!-- /.col -->
            </div>
            <!-- /.row -->
        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->

</div>
<!-- ./wrapper -->

<div class="modal fade" id="confirm-edit" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                修改测试
            </div>
            <div class="modal-body">
                <form id="form_data" role="form" class="form-horizontal">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">测试名称</label>
                        <div class="col-sm-10">
                            <input name="classname" class="form-control">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 control-label">测试描述</label>
                        <div class="col-sm-10">
                            <textarea name="classdescri" class="form-control" rows="3"></textarea>
                        </div>
                    </div>

                    <div class="form-group isnowpub">
                        <label class="col-sm-3 control-label" style="text-align:left;">是否现在发布</label>
                        <div class="col-md-6" style="margin-left: -46px">
                            <input name="ispublishnow" type="checkbox">
                        </div>
                    </div>
                    <div class="form-group" style="margin-left:5px;">
                        <label>起始时间至结束时间</label>

                        <div class="col-sm-10 input-group">
                            <div class="input-group-addon">
                                <i class="fa fa-clock-o"></i>
                            </div>
                            <input type="text" disabled class="  timerange col-sm-6 form-control pull-right"
                                   id="reservationtime">
                        </div>

                    </div>

                    <div class="form-group" style="margin-left:-20px;">
                        <label class="col-sm-2 control-label">发布班级</label>
                        <div class="col-md-6">
                            <select disabled class="pubclasses  form-control select2 select2-hidden-accessible"
                                    style="width: 100%;"
                                    tabindex="-1" aria-hidden="true">

                            </select>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default cancelclas " data-dismiss="modal">取消</button>
                <a class="btn btn-primary btn-okque">确定</a>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="confirm-pub" tabindex="-1" role="dialog" aria-labelledby="myModalLabel2"
     aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                请确认
            </div>
            <div class="modal-body">
                <form id="form_data" role="form" class="form-horizontal">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">输入账号密码</label>
                        <div class="col-sm-6">
                            <input name="password" class="form-control">
                        </div>
                        <div class="col-sm-2">
                            <button type="button" class="checkuser btn btn-block btn-info btn-sm">验证</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class=" btn btn-default cancel" data-dismiss="modal">取消</button>
                <a class="disabled btn btn-danger btn-ok deleteclass">取消发布</a>
            </div>
        </div>
    </div>
</div>
<!-- jQuery 3 -->
{{/*<script src="/static/libs/jquery/jquery.min.js"></script>*/}}
<!-- Bootstrap 3.3.7 -->
{{template "admin/page_end.html"}}
{{/*<script src="/static/libs/bootstrap/js/bootstrap.min.js"></script>*/}}
<!-- DataTables -->
<script src="/static/libs/datatables.net/js/jquery.dataTables.min.js"></script>
<script src="/static/libs/datatables.net-bs/js/dataTables.bootstrap.min.js"></script>
<!-- AdminLTE App -->
{{/*<script src="/static/libs/AdminLTE/js/adminlte.min.js"></script>*/}}
<!-- page script -->
<script>

    // var data = [{id: 0, text: '简单'}, {id: 1, text: '中等'}, {id: 2, text: '困难'}];
    $(".pubclasses").select2({
        // data: data,
        placeholder: '请选择',
        allowClear: true,
        multiple: true,
    });
    var ispublishnow = false;
    var flag = "";
    var cid;
    var time = "";
    var cname;
    var cdes;
    var code;
    var issave = false;
    var displaytime = "";
    var custom = [
        'bangTidy',
        'blackgloss'
    ];
    $(function () {
        $('#example2').DataTable();
        $('#example3').DataTable({
            'paging': true,
            'lengthChange': true,
            'searching': true,
            'ordering': true,
            'info': true,
            'autoWidth': false
        });
    });
    $(function () {

    });
    $(function () {
        $('input[name="ispublishnow"]').iCheck({
            checkboxClass: 'icheckbox_flat-orange',
            radioClass: 'iradio_flat-orange'
        }).on('ifChecked', function (event) {
            ispublishnow = true;
            $('.timerange').attr("disabled", false);
            $('.pubclasses').attr("disabled", false);
        }).on('ifUnchecked', function (event) {
            ispublishnow = false;
            $('.timerange').attr("disabled", true);
            $('.pubclasses').attr("disabled", true);
        });
    });

    //Date range picker
    $('#reservation').daterangepicker();
    //Date range picker with time picker
    $('#reservationtime').daterangepicker({
        timePickerIncrement: 60, //时间的增量，单位为分钟
        opens: 'right', //日期选择框的弹出位置
        timePicker12Hour: false, //是否使用12小时制来显示时间
        ranges: {
            '最近1小时': [moment(), moment().add('hours', 1)],
            '今日内': [moment(), moment().add('days', 1).startOf('day')],
            '明日内': [moment().add('days', 1).startOf('day'), moment().add('days', 2).startOf('day')],
            '7日内': [moment(), moment().add('days', 6)],
            '30日内': [moment(), moment().add('days', 29)]
        },
        format: 'YYYY-MM-DD HH:mm',
        timePicker: true, timePickerIncrement: 30, "locale": {
            format: 'YYYY-MM-DD HH:mm',
            applyLabel: "应用",
            cancelLabel: "取消",
            resetLabel: "重置",
        }
    }, function (start, end, label) {
        //格式化日期显示框
        $('#searchDate').val(start.format('YYYY-MM-DD HH:mm') + '-' + end.format('YYYY-MM-DD HH:mm'));
    });

    function getpubclassesbyid(id) {
        $.post("/admin/getpubclass", {
            examid: id
        }, function (result) {
            if (result.succeed) {
                var strarr = []
                console.log(result);
                for (var j = 0; j < result.classes.length; j++) {
                    strarr.push("" + result.classes[j].id)
                }
                $(".pubclasses").val(strarr).trigger("change");
                // $(".pubclasses").select2({
                //     data: result.classes,
                //     placeholder: '请选择',
                //     allowClear: true,
                //     multiple: true
                // });
            } else {
                $('.top-right').notify({
                    message: {text: '网络有问题,获取班级失败'},
                    type: custom[1]
                }).show();
            }
        }, "json")
    }


    function getclasses() {
        $.get("/admin/getclasses", function (result) {
            console.log(result);
            if (result.succeed) {

                $(".pubclasses").select2({
                    data: result.classes,
                    placeholder: '请选择',
                    allowClear: true,
                    multiple: true,
                });


            } else {
                $('.top-right').notify({
                    message: {text: '网络有问题,获取班级失败'},
                    type: custom[1]
                }).show();
                $('input[name="quenums"]').focus();
                return;
            }
        })
    }

    $('#confirm-edit').on('show.bs.modal', function (e) {
        cid = $(e.relatedTarget).data('id');
        flag = $(e.relatedTarget).data('flag');
        var myid = $('td[name=' + cid + ']');
        console.log(cid);
        cname = $.trim(myid.next().text());
        getclasses();
        getpubclassesbyid(cid);
        cdes = $.trim(myid.next().next().text());
        // $(this).find('.btn-ok').unbind("click");
        // cname = $(e.relatedTarget).data('name');
        // cdes = $(e.relatedTarget).data('desc');
        // console.log(cname,cdes);
        // code = $(e.relatedTarget).data('code');
        if (flag !== "unpub") {
            st = $(e.relatedTarget).data('st');
            et = $(e.relatedTarget).data('et');
            displaytime = st + " - " + et;
            console.log(displaytime);
            $('.timerange').val(displaytime);

            $(".isnowpub").css("display", "none");
            $('.timerange').attr("disabled", false);
            $('.pubclasses').attr("disabled", false);
        } else {
            $(".isnowpub").css("display", "");
            $('.timerange').attr("disabled", true);
            $('.pubclasses').attr("disabled", true);
        }


        $('input[name="classname"]').val(cname);
        $('textarea[name="classdescri"]').val(cdes);


    })
    ;
    $('.cancelclas').click(function () {
        // $(".pubclasses").html("");
        console.log($('.timerange').val());
        $('.timerange').val("");
        $('input[name="ispublishnow"]').iCheck('uncheck');
    });

    $('.btn-okque').click(function () {
        var examname = $('input[name="classname"]').val();
        var examdescrib = $('textarea[name="classdescri"]').val();
        var timerange = $('.timerange').val();
        var pubclassres = $(".pubclasses").select2("data"); //多选
        var idstr = cid + "";
        var pubclass = "";

        if (flag == "unpub" && !ispublishnow) {


        } else {
            if (pubclassres.length == 0) {
                $('.top-right').notify({
                    message: {text: '发布班级不能为空'},
                    type: custom[1]
                }).show();
                // $('input[name="testname"]').focus();
                return;
            }

            if (!timerange) {
                $('.top-right').notify({
                    message: {text: '发布时间不能为空！'},
                    type: custom[1]
                }).show();
                $('.timerange').focus();
                return;
            }
            var ids = [];
            for (var j = 0; j < pubclassres.length; j++) {
                ids.push(pubclassres[j].id)
            }
            pubclass = ids.join(",");

            time = timerange;
        }


        if (!examname) {
            $('.top-right').notify({
                message: {text: '测试名称不能为空！'},
                type: custom[1]
            }).show();
            $('input[name="classname"]').focus();
            return;
        }

        //修改班级信息
        $.post("/admin/exam/update", {
            examname: examname,
            examdescrib: examdescrib,
            cid: cid,
            timerange: time,
            pubclass: pubclass,
        }, function (result) {
            console.log(result);
            if (result.succeed) {
                $('.top-right').notify({
                    message: {text: '修改成功'},
                    type: custom[1]
                }).show();
                window.location.href = window.location.href;
                // var tem = $('td[name=' + idstr + ']').next().text(classname).next().text(classdescrib)
                // if (flag == "unpub") {
                // tem.next().next().text(result.exam.)
                // }
                $('#confirm-edit').modal("hide");
            } else {
                $('.top-right').notify({
                    message: {text: "修改失败，原因：" + result.message},
                    type: custom[1]
                }).show();
            }
        }, 'json');
    });

    $('#confirm-pub').on('show.bs.modal', function (e) {
        console.log("jinlaile");
        var cid = $(e.relatedTarget).data('id');
        var userid = {{.user.ID}}+"";
        var delethis = $(this).find('.deleteclass');
        var checkthis = $(this).find('.checkuser');
        $(this).find('.checkuser').click(function () {
            var password = $('input[name="password"]').val();
            password = $.trim(password);

            if (password.length == 0) {
                $('.top-right').notify({
                    message: "密码不能为空",
                    type: custom[1]
                }).show();
                return
            }

            $.post("/admin/checkuser", {userid: userid, password: password}, function (result) {

                if (result.succeed) {

                    $('.top-right').notify({
                        message: {text: result.message},
                        type: custom[1]
                    }).show();
                    checkthis.unbind("click");
                    issave = true;
                    delethis.removeClass("disabled");
                } else {
                    $('.top-right').notify({
                        message: {text: result.message},
                        type: custom[1]
                    }).show();
                }
            }, 'json');

        });


        $(this).find('.deleteclass').click(function () {
            if (issave) {
                $.post("/admin/exam/unpublish", {examid: cid}, function (result) {
                    console.log(result);
                    if (result.succeed) {
                        $('.top-right').notify({
                            message: {text: result.message},
                            type: custom[1]
                        }).show();
                        $('input[name="password"]').val("");
                        $("#confirm-delete").modal("hide");
                        delethis.addClass("disabled");
                        delethis.unbind("click");
                        checkthis.unbind("click");
                        window.location.href = window.location.href;
                        issave = false;
                    } else {
                        $('.top-right').notify({
                            message: {text: result.message},
                            type: custom[1]
                        }).show();
                        delethis.unbind("click");
                        checkthis.unbind("click");
                    }
                }, 'json');
            } else {
                $('.top-right').notify({
                    message: "未验证密码",
                    type: custom[1]
                }).show();
            }
        });

        $(this).find('.cancel').click(function () {
            delethis.unbind("click");
            checkthis.unbind("click");
        });


    });

</script>
</body>
</html>
{{end}}